{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0640904-5e38-40a1-9270-a3411e29e63e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\HP\\\\Desktop\\\\部分国家GDP可视化.html'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd  \n",
    "from pyecharts import options as opts  \n",
    "from pyecharts.charts import Map, Timeline  \n",
    "\n",
    "df = pd.read_excel(\"C:\\\\Users\\\\HP\\\\Desktop\\\\GDP (E) .xlsx\")  \n",
    "for col in df.columns[1:]:  \n",
    "    df[col] = pd.to_numeric(df[col], errors='coerce')  \n",
    "df = df.dropna(subset=df.columns[1:])  \n",
    "years_columns = df.columns[1:]  \n",
    "timeline = Timeline()  \n",
    "timeline.add_schema(  \n",
    "    play_interval=1000, \n",
    "    is_auto_play=True,\n",
    "    is_timeline_show=True  \n",
    ")  \n",
    "for year_column in years_columns:  \n",
    "    year_data = df[['国家', year_column]]  \n",
    "    \n",
    "    map_chart = (  \n",
    "        Map()  \n",
    "        .add(  \n",
    "            series_name=f\"{year_column}年GDP\",  \n",
    "            data_pair=[list(z) for z in zip(year_data['国家'], year_data[year_column])], \n",
    "            maptype='world',   \n",
    "            label_opts=opts.LabelOpts(is_show=False),    \n",
    "            is_map_symbol_show=False  \n",
    "        )  \n",
    "        .set_global_opts(  \n",
    "            title_opts=opts.TitleOpts(title=f\"{year_column}年各国GDP分布\"),  \n",
    "            visualmap_opts=opts.VisualMapOpts(  \n",
    "                is_piecewise=True, \n",
    "                pieces=[  \n",
    "                    {\"min\": min_value, \"max\": max_value, \"label\": f\"{min_value} - {max_value}\"}  \n",
    "                    for min_value, max_value in zip(  \n",
    "                        [None] + list(df[year_column].quantile([0.2, 0.4, 0.6, 0.8]).tolist()),  \n",
    "                        list(df[year_column].quantile([0.2, 0.4, 0.6, 0.8]).tolist()) + [None]  \n",
    "                    )  \n",
    "                ],  \n",
    "                is_show=True  \n",
    "            )  \n",
    "        )  \n",
    "    )      \n",
    "    timeline.add(map_chart, year_column)  \n",
    "timeline.render('部分国家GDP可视化.html')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d86cf6aa-3fa3-4af2-9044-802c0211d277",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
